package com.youmingxing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.sso.UMSsoHandler;
import com.youmingxing.R;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.utils.DisplayUtil;
import com.youmingxing.utils.LogUtil;
import com.youmingxing.utils.Tools;
import com.youmingxing.wxapi.UMManger;

import org.json.JSONObject;

/**
 * JHS
 * 2015/12/27
 */

public class WorksCommitSucceedActivity extends BaseActivity implements View.OnClickListener {

    private TextView shareCircle;
    private TextView shareWechat;
    private TextView shareWeibo;
    private TextView shareQQ;

    private TextView shareContet;
    private ImageView worksImg;

    private UMManger manger;

    int entrance;//来源页面
    /**
     * entrance为1时，传用户ID；
     * entrance为2时，传 海选ID；
     * entrance为3时，传 发布的动态ID
     */
    private int id;//用户id,海选id,或作品id
    private String content;//用户id,海选id,或作品id
    final int TYPE_QQ = 3;
    final int TYPE_WEIXIN = 1;
    final int TYPE_CIRCLE = 2;
    final int TYPE_WEIBO = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_workscommit_succeed);
        initView();
        getActivityIntent();
    }


    private void initView() {
        setTopbarName("上传成功");
        setLeftAndrightTv(true, true, false, R.string.finish, null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }, null);
        shareCircle = (TextView) findViewById(R.id.share_wechat_moment);
        shareWechat = (TextView) findViewById(R.id.share_wechat);
        shareQQ = (TextView) findViewById(R.id.share_qq);
        shareWeibo = (TextView) findViewById(R.id.share_micbloge);
        shareContet = (TextView) findViewById(R.id.works_hint_tv);

        worksImg = (ImageView) findViewById(R.id.works_img);
        worksImg.setLayoutParams(new LinearLayout.LayoutParams(
                DisplayUtil.getScreenWidth(),
                (DisplayUtil.getScreenWidth()- Tools.dip2px(this,70)) * 9 / 16));
        shareCircle.setOnClickListener(this);
        shareWechat.setOnClickListener(this);
        shareQQ.setOnClickListener(this);
        shareWeibo.setOnClickListener(this);
    }

    private void getActivityIntent() {
        id = getIntent().getExtras().getInt("id");
        entrance = getIntent().getExtras().getInt("entrance");
        content = getIntent().getExtras().getString("content");
        manger = UMManger.getInstance();
        manger.getInstance(WorksCommitSucceedActivity.this);

        SelfInfoApi.getShareContent(2, id, entrance, new BaseJsonHandler(this) {
            @Override
            public void onSuccessWithData(JSONObject response) {
                JSONObject data = response.optJSONObject("data");
                LogUtil.i("---------succeed---","" +data.optString("img"));
                Glide.with(WorksCommitSucceedActivity.this).load(data.optString("img"))
                        .crossFade().into(worksImg);
                shareContet.setText(content);
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = manger.mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_wechat_moment:
                manger.shareContent(id, entrance, TYPE_CIRCLE, SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.share_wechat:
                manger.shareContent(id, entrance, TYPE_WEIXIN, SHARE_MEDIA.WEIXIN);
                break;
            case R.id.share_qq:
                manger.shareContent(id, entrance, TYPE_QQ, SHARE_MEDIA.QQ);
                break;
            case R.id.share_micbloge:
                manger.shareContent(id, entrance, TYPE_WEIBO, SHARE_MEDIA.SINA);
                break;
        }
    }
}
